package projectodedalo.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import projectodedalo.modelo.Idea;
import projectodedalo.negocio.GestionIdea;
import projectodedalo.negocio.GestionUsuario;

/**
 * Servlet implementation class CargarIdeasServlet
 */
@WebServlet("/CargarIdeasServlet")
public class CargarIdeasServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CargarIdeasServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("CargarIdeasServlet");
		int xcodigo = 0;

		try {
			GestionIdea idea = new GestionIdea();
			request.setAttribute("lstIdeas", idea.listarIdeas());
			
			ArrayList<Idea> objIdea = new ArrayList<Idea>();
			objIdea = (ArrayList<Idea>) idea.listarIdeas();
			System.out.println(objIdea.size());
			
			if (objIdea.size()!=0) {
				request.setAttribute("codigoMsg", "OK");
				request.setAttribute("servletMsg", "Listado de Ideas del usuario...");
			} else {
				request.setAttribute("codigoMsg", "ERROR");
				request.setAttribute("servletMsg", "Usted no tiene Ideas asignadas...");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			request.setAttribute("servletMsg", e.getMessage());
			
		}

		RequestDispatcher rd = request.getRequestDispatcher("forumIdeas.jsp");
		rd.forward(request, response);
	}

}
